package com.yidao.customer.controller;

import javax.validation.Valid;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.validation.FieldError;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.view.json.MappingJacksonJsonView;

import com.yidao.core.controller.BaseController;
import com.yidao.core.domain.SearchCriteria;
import com.yidao.core.domain.SearchExpression;
import com.yidao.core.domain.SearchOperator;
import com.yidao.customer.domain.Customer;
import com.yidao.customer.service.CustomerService;

@Controller
@RequestMapping("/customers")
public class CustomerController extends BaseController{

	@Autowired
	private CustomerService customerService;
	
	@RequestMapping(value="/new", method = RequestMethod.GET)
    public ModelAndView editNew() {
		ModelAndView mv = new ModelAndView();
		mv.addObject("customer", new Customer());
		mv.addObject("genderOptions", Customer.genderOptions);
		mv.setViewName("/customers/edit");
		return mv;
    }
	
	@RequestMapping(value="/{id}/edit", method = RequestMethod.GET)
    public ModelAndView edit(@PathVariable long id) {
		Customer customer = this.customerService.getById(id);
		ModelAndView mv = new ModelAndView();
		mv.addObject("customer", customer);
		mv.addObject("genderOptions", Customer.genderOptions);
		mv.setViewName("/customers/edit");
		return mv;
	}
	
	@RequestMapping(value="/{id}/delete", method = RequestMethod.GET)
    public ModelAndView delete(@PathVariable long id) {
		customerService.deleteById(id);
		return this.list();
	}
	
	@RequestMapping(value="/{id}/print", method = RequestMethod.GET)
    public ModelAndView print(@PathVariable long id) {
		Customer customer = customerService.getById(id);
		ModelAndView mv = new ModelAndView("/customers/print");
		mv.addObject("customer",customer);
		return mv;
	}
	
	@RequestMapping(value="/search", method = RequestMethod.GET)
    public ModelAndView searchForm(@ModelAttribute("customerSearchCriteria") SearchCriteria customerSearchCriteria) {
		ModelAndView mv = new ModelAndView();
		mv.addObject("customerSearchCriteria",customerSearchCriteria);
		mv.setViewName("/customers/search");
		return mv;
	}
	
	@ModelAttribute("customerSearchCriteria")
	public SearchCriteria createCustomerSearchCriteria(){
		SearchCriteria customerSearchCriteria = new SearchCriteria();
		customerSearchCriteria.addExpression(new SearchExpression("name",SearchOperator.eq));
		customerSearchCriteria.addExpression(new SearchExpression("jobTitle",SearchOperator.eq));
		return customerSearchCriteria;
	}
	
	@RequestMapping(value="/search", method = RequestMethod.POST)
    public ModelAndView search(@ModelAttribute("customerSearchCriteria") @Valid SearchCriteria customerSearchCriteria,
    		BindingResult result) {
		System.out.println("customerSearchCriteria:" + customerSearchCriteria);
		ModelAndView mv = new ModelAndView();
		mv.addObject("customers",customerService.list(customerSearchCriteria));
		mv.setViewName("/customers/list");
		return mv;
	}
	
	@RequestMapping(method = RequestMethod.GET)
	public ModelAndView list(){
		ModelAndView mv = new ModelAndView();
		mv.addObject("customers",customerService.listAll());
		mv.setViewName("/customers/list");
		return mv;
	}
	
	@RequestMapping(value="list.json",method = RequestMethod.GET)
	public ModelAndView listJsonData(){
		ModelAndView mv = new ModelAndView();
		mv.addObject("customers",customerService.listAll());
		MappingJacksonJsonView jsonView = new MappingJacksonJsonView();
		mv.setView(jsonView);
		return mv;
	}
	
	@RequestMapping(method = RequestMethod.POST)
    public ModelAndView save(@ModelAttribute("customer") @Valid Customer customer, 
    							BindingResult result) {
        if (result.hasErrors()) {
        	System.out.println("===field error here.===");
        	for(FieldError fieldError:result.getFieldErrors()){
        		System.out.println(fieldError.getCode());
        		System.out.println(fieldError.getField());
        		System.out.println(fieldError.getDefaultMessage());
        	}
        	ModelAndView mv = new ModelAndView();
    		mv.addObject("fieldErrors", result.getFieldErrors());
        	mv.addObject("customer", new Customer());
        	mv.addObject("genderOptions", Customer.genderOptions);
    		mv.setViewName("/customers/edit");
    		return mv;
        }
        
        if(customer.getId() == null){
        	customerService.create(customer);
        }else{
        	Long id = customer.getId();
        	Customer destCustomer = customerService.getById(id);
        	destCustomer.setJobTitle(customer.getJobTitle());
        	destCustomer.setGender(customer.getGender());
        	destCustomer.setName(customer.getName());
        	destCustomer.setBirthday(customer.getBirthday());
        	customerService.update(destCustomer);
        }
        return list();
    }
}
